package ptumall.controller;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ptumall.model.HealthArticle;
import ptumall.service.ArticleService;
import ptumall.utils.ApiResponse;

@RestController
@RequestMapping("/api/articles")
@RequiredArgsConstructor
public class ArticleController {
    private final ArticleService articleService;
    //文章查询
    @GetMapping
    public ApiResponse<PageInfo<HealthArticle>> getArticles(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ApiResponse.success(articleService.getArticles(page, size));
    }

    //文章详情
    @GetMapping("/detail/{id}") // 添加路径参数占位符
    public ApiResponse<HealthArticle> getDetail(@PathVariable Long id) {
        return ApiResponse.success(articleService.getDetail(id));
    }

    //文章搜索
    @GetMapping("/search")
    public ApiResponse<PageInfo<HealthArticle>> search(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return ApiResponse.success(articleService.searchArticles(keyword, page, size));
    }
}
